package st.business.tepth.business.maintainplan;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import st.business.tepth.R;
import st.business.tepth.R2;
import st.business.tepth.business.history.detail.HistoryDetailDelegate;
import st.shadow.library.core.base.fragment.BaseFragment;
import st.shadow.library.core.util.utilcode.util.DateUtils;
import st.shadow.library.ui.indicator.magicindicator.MagicIndicator;
import st.shadow.library.ui.indicator.magicindicator.ViewPagerHelper;
import st.shadow.library.ui.indicator.magicindicator.buildins.UIUtil;
import st.shadow.library.ui.indicator.magicindicator.buildins.commonnavigator.CommonNavigator;
import st.shadow.library.ui.indicator.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import st.shadow.library.ui.indicator.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import st.shadow.library.ui.indicator.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import st.shadow.library.ui.indicator.magicindicator.buildins.commonnavigator.indicators.WrapPagerFillIndicator;
import st.shadow.library.ui.indicator.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView;
import st.shadow.library.ui.indicator.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;
import st.shadow.library.ui.indicator.magicindicator.buildins.commonnavigator.titles.badge.BadgePagerTitleView;

/**
 * Author: shadow
 * Email:  1025431817@qq.com
 * Time:   2019-06-04 16:04
 */

public class MaintainPlanDelegate extends BaseFragment {

    public static MaintainPlanDelegate newInstance() {
        return new MaintainPlanDelegate();
    }

    @BindView(R2.id.indicator)
    MagicIndicator mIndicator;
    @BindView(R2.id.view_pager_bus)
    ViewPager mViewPager;
    @BindView(R2.id.tv_tile)
    TextView mTvTitle;

    @Override
    public Object setLayout() {
        return R.layout.delegate_maintain_plan;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        EventBus.getDefault().register(this);
        mTvTitle.setText("保养计划");
        final List<String> list = DateUtils.getFutureDays(8);
        CommonNavigator commonNavigator = new CommonNavigator(getContext());
        commonNavigator.setLeftPadding(0);
        commonNavigator.setRightPadding(0);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return list.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                String date = list.get(index);
                final BadgePagerTitleView badgePagerTitleView = new BadgePagerTitleView(context);
                SimplePagerTitleView simplePagerTitleView =
                        new ColorTransitionPagerTitleView(context);
                simplePagerTitleView.setText(date);
                simplePagerTitleView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 11);
                simplePagerTitleView.setGravity(Gravity.CENTER);
                simplePagerTitleView.setBackground(context.getResources()
                        .getDrawable(R.drawable.shape_solid_trans_with_yellow_stroke));
                simplePagerTitleView.setNormalColor(Color.parseColor("#333333"));
                simplePagerTitleView.setSelectedColor(Color.parseColor("#333333"));
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mViewPager.setCurrentItem(index);
                        badgePagerTitleView.setBadgeView(null); // cancel badge when click tab
                    }
                });
                badgePagerTitleView.setInnerPagerTitleView(simplePagerTitleView);
                return badgePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                WrapPagerFillIndicator indicator = new WrapPagerFillIndicator(context);
                indicator.setRoundRadius(0);
                indicator.setHorizontalPadding(UIUtil.dip2px(context, 20));
                indicator.setFillColor(Color.parseColor("#f1b647"));
                return indicator;
            }
        });
        mViewPager.setAdapter(new MaintainPlanPagerAdapter(getChildFragmentManager(),
                list));
        mIndicator.setNavigator(commonNavigator);
        ViewPagerHelper.bind(mIndicator, mViewPager);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe
    public void onStartBrotherEvent(ChildEvent event) {
        start(HistoryDetailDelegate
                .newInstance(event.repairId));
    }

    @OnClick({R2.id.tv_back})
    public void onBack() {
        pop();
    }
}